/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.thoughtbot.expandablecheckrecyclerview.multicheck;

import com.thoughtbot.expandablecheckrecyclerview.ResourceTable;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.List;
import java.util.Locale;

/**
 * MuCheckChildProvider
 *
 * @since 2021-07-16
 */
public class MuCheckChildProvider extends BaseItemProvider {
    // ListContainer的数据集合
    private List<MuCheckChildItem> childList;
    private Context context;

    /**
     * MuCheckChildProvider
     *
     * @param list list
     * @param context context
     */
    public MuCheckChildProvider(List<MuCheckChildItem> list, Context context) {
        this.childList = list;
        this.context = context;
    }

    // 用于保存列表项中的子组件信息
    /**
     * SettingHolder
     *
     * @since 2021-07-16
     */
    public static class SettingHolder {
        Text settingText;
        Checkbox checkbox;

        SettingHolder(Component component) {
            settingText = (Text) component.findComponentById(ResourceTable.Id_multi_text);
            checkbox = (Checkbox) component.findComponentById(ResourceTable.Id_checkbox);
        }
    }

    @Override
    public int getCount() {
        return childList == null ? 0 : childList.size();
    }

    @Override
    public Object getItem(int position) {
        if (childList != null && position >= 0 && position < childList.size()) {
            return childList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        SettingHolder holder;
        MuCheckChildItem setting = childList.get(position);
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_multicheck_child_item_lib, null, false);
            holder = new SettingHolder(cpt);

            // 将获取到的子组件信息绑定到列表项的实例中
            cpt.setTag(holder);
        } else {
            cpt = component;

            // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
            holder = (SettingHolder) cpt.getTag();
        }
        holder.settingText.setText(setting.getSettingName().toUpperCase(Locale.ROOT));
        holder.checkbox.setChecked(setting.isChecked());
        setting.setCheckbox(holder.checkbox);
        return cpt;
    }
}
